package com.hermit.qmst.UI.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.hermit.qmst.tools.Log;

/**
 * Created by hermithermit on 15/2/15.
 */
public class AlphaView extends View{

    private String[] alphas = { "#", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
    private OnTouchingAlphaChangedListener mOnTouchingAlphaChangedListener;
    //初始化画笔
    private Paint mPaint = new Paint();
    //选中索引
    private int mChoose = -1;
    //是否显示
    private boolean mShowBg = false;

    public AlphaView(Context context) {
        super(context);
    }

    public AlphaView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public AlphaView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {

        Log.i("AlphaView","dispatchTouchEvent",true);
        //事件触摸发生的Y值
        float y = event.getY();
        //计算选中的字母索引
        int index = (int)(y / getHeight() * alphas.length);

        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                if (mOnTouchingAlphaChangedListener != null
                        && (index >= 0) && (index < alphas.length))
                {
                    mOnTouchingAlphaChangedListener.onTouchingAlphaChanged(alphas[index]);
                }

                mChoose = index;
                //invalidate()会调用onDraw办法
                invalidate();
                mShowBg = true;
                return true;
            case MotionEvent.ACTION_UP:

                mShowBg = false;
                mChoose = -1;
                invalidate();
                return true;
            default:
                break;
        }
        return super.dispatchTouchEvent(event);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Log.i("AlphaView","onDraw",true);
        if (mShowBg){
            canvas.drawColor(Color.parseColor("#40000000"));
        }

        int viewHeight = getHeight();
        int viewWidth = getWidth();
        int alphaH = viewHeight / alphas.length;

        for (int i = 0;i < alphas.length; i++){

            mPaint.setColor(-1);
            mPaint.setTypeface(Typeface.DEFAULT_BOLD);
            mPaint.setAntiAlias(true);
            mPaint.setTextSize(30);

            if (i == mChoose){
                mPaint.setColor(Color.parseColor("#ff1a9c78"));
                mPaint.setFakeBoldText(true);
            }
            float x = viewWidth / 2 - mPaint.measureText(alphas[i]) / 2.0f;
            float y = alphaH + alphaH * i;

            canvas.drawText(alphas[i],x,y,mPaint);
            mPaint.reset();
        }

    }

    public void setOnTouchingAlphaChangedListener(OnTouchingAlphaChangedListener listener){
        mOnTouchingAlphaChangedListener = listener;
    }

    //定义字母变化监听接口
    public static abstract interface OnTouchingAlphaChangedListener{
        public abstract void onTouchingAlphaChanged(String alpha);
    }

}
